package com.example.navigation;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.TukelaExampleDemo.R;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author he xiaohui
 *
 */
public class loopViewPagerActivity extends Activity
{
	ViewPager viewpager;

	private TextView index_textview;

	private int[] pics = { R.drawable.p1, R.drawable.p2, R.drawable.p3,
			R.drawable.p4 };

	private Runnable viewpagerRunnable;

	private static Handler handler;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_loopviewpager);
		viewpager = (ViewPager) findViewById(R.id.viewpager);
		index_textview = (TextView) findViewById(R.id.index_textview);
		index_textview.setText("the 1 page");
		handler = new Handler();

		// 添加事件监听
		initListener();

		int len = pics.length;
		View view = null;
		ImageView imageview;
		LayoutInflater inflater = LayoutInflater.from(this);
		List<View> lists = new ArrayList<View>();
		for (int i = 0; i < len; i++)
		{
			view = inflater.inflate(R.layout.imageview_layout, null);
			imageview = (ImageView) view.findViewById(R.id.imageview);
			imageview.setBackgroundResource(pics[i]);
			lists.add(view);
		}
		viewpager.setAdapter(new ViewPagerAdapter(lists));

		// 开启自动切换图片
		initRunnable();
	}

	/**
	 * 添加事件监听
	 */
	private void initListener()
	{
		viewpager.setOnPageChangeListener(new OnPageChangeListener()
		{
			boolean isScrolled = false;

			@Override
			public void onPageScrollStateChanged(int status)
			{
				switch (status)
				{
					case 1:// 手势滑动
						isScrolled = false;
						break;
					case 2:// 界面切换
						isScrolled = true;
						break;
					case 0:// 滑动结束

						// 当前为最后一张，此时从右向左滑，则切换到第一张
						if (viewpager.getCurrentItem() == viewpager.getAdapter()
								.getCount() - 1 && !isScrolled)
						{
							viewpager.setCurrentItem(0);
						}
						// 当前为第一张，此时从左向右滑，则切换到最后一张
						else if (viewpager.getCurrentItem() == 0 && !isScrolled)
						{
							viewpager.setCurrentItem(viewpager.getAdapter()
									.getCount() - 1);
						}
						break;
				}
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2)
			{

			}

			@Override
			public void onPageSelected(int index)
			{
				index_textview.setText("the " + (index + 1) + " page");
			}
		});
	}

	private static final int TIME = 2500;

	/**
	 * 定时切换
	 */
	protected void initRunnable()
	{
		viewpagerRunnable = new Runnable()
		{

			@Override
			public void run()
			{
				int nowIndex = viewpager.getCurrentItem();
				int count = viewpager.getAdapter().getCount();
				// 如果下一张的索引大于最后一张，则切换到第一张
				if (nowIndex + 1 >= count)
				{
					viewpager.setCurrentItem(0);
				} else
				{
					viewpager.setCurrentItem(nowIndex + 1);
				}
				handler.postDelayed(viewpagerRunnable, TIME);
			}
		};
		handler.postDelayed(viewpagerRunnable, TIME);
	}



	class ViewPagerAdapter extends PagerAdapter
	{

		private List<View> list;

		public ViewPagerAdapter(List<View> list)
		{
			this.list = list;
		}

		@Override
		public void destroyItem(View view, int position, Object arg2)
		{
			ViewPager pViewPager = ((ViewPager) view);
			pViewPager.removeView(list.get(position));
		}

		@Override
		public void finishUpdate(View arg0)
		{
		}

		@Override
		public int getCount()
		{
			return list.size();
		}

		@Override
		public Object instantiateItem(View view, int position)
		{
			ViewPager pViewPager = ((ViewPager) view);
			pViewPager.addView(list.get(position));
			return list.get(position);
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1)
		{
			return arg0 == arg1;
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1)

		{
		}

		@Override
		public Parcelable saveState()
		{
			return null;
		}

		@Override
		public void startUpdate(View arg0)
		{
		}
	}

}
